import { reactive } from 'vue'

// 简单的状态管理
const state = reactive({
  // 全局加载状态
  loading: {
    global: false,
    cameras: false,
    alerts: false
  },
  
  // 侧边栏状态
  sidebar: {
    collapsed: false
  },
  
  // 用户信息
  user: {
    loggedIn: false,
    info: null
  },
  
  // 系统统计数据
  statistics: {
    onlineCameras: 0,
    totalAlerts: 0,
    unprocessedAlerts: 0,
    processingRate: 0
  },
  
  // 活动告警
  activeAlerts: [],
  
  // 通知消息
  notifications: []
})

// 操作方法
const actions = {
  // 设置加载状态
  setLoading(type, value) {
    if (type === 'global') {
      state.loading.global = value
    } else if (state.loading.hasOwnProperty(type)) {
      state.loading[type] = value
    }
  },
  
  // 切换侧边栏
  toggleSidebar() {
    state.sidebar.collapsed = !state.sidebar.collapsed
  },
  
  // 设置用户信息
  setUser(userInfo) {
    state.user.loggedIn = !!userInfo
    state.user.info = userInfo
  },
  
  // 更新统计数据
  updateStatistics(stats) {
    state.statistics = { ...state.statistics, ...stats }
  },
  
  // 添加通知
  addNotification(notification) {
    const id = Date.now()
    const newNotification = {
      id,
      ...notification,
      timestamp: new Date().toISOString()
    }
    state.notifications.push(newNotification)
    
    // 自动删除超时通知
    if (notification.autoDismiss !== false) {
      setTimeout(() => {
        this.removeNotification(id)
      }, notification.dismissTimeout || 5000)
    }
    
    return id
  },
  
  // 移除通知
  removeNotification(id) {
    const index = state.notifications.findIndex(n => n.id === id)
    if (index > -1) {
      state.notifications.splice(index, 1)
    }
  },
  
  // 清空通知
  clearNotifications() {
    state.notifications = []
  },
  
  // 更新活动告警
  updateActiveAlerts(alerts) {
    state.activeAlerts = alerts
  }
}

export default {
  state,
  actions
}